//
//  ZCLogisticsVC.m
//  ZCJZ
//
//  Created by sujeking on 2019/3/7.
//  Copyright © 2019年 jeking. All rights reserved.
//

#import "ZCLogisticsVC.h"

#import "ZCOrderCheckSectionHeadView.h"

#import "ZCLogisticsExpressInfoCell.h"
#import "ZCLogisticsSetipCell.h"
#import "ZCLogisticsitemCell.h"

#import "ZCOrderViewModel.h"
#import <WebKit/WebKit.h>
#import <Masonry.h>



@interface ZCLogisticsVC ()<WKNavigationDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, copy) NSDictionary *datasource;

@property (nonatomic, strong) WKWebView *webview;


@end

@implementation ZCLogisticsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
//    [self netFetchLogistiscInfo];
}

- (void)setupUI {
    self.navTitle = @"物流信息";
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCLogisticsExpressInfoCell" bundle:nil]
         forCellReuseIdentifier:@"ZCLogisticsExpressInfoCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCLogisticsSetipCell" bundle:nil]
         forCellReuseIdentifier:@"ZCLogisticsSetipCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCLogisticsitemCell" bundle:nil]
         forCellReuseIdentifier:@"ZCLogisticsitemCell"];
    
    self.webview = [[WKWebView alloc] init];
    [self.view addSubview:self.webview];
    [self.webview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.tableView);
    }];
    self.webview.navigationDelegate = self;
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *urlstr = [NSString stringWithFormat:@"http://page.cainiao.com/guoguo/app-myexpress-taobao/express-detail.html?mailNo=%@&cpCode=",self.logisticId];
    [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]]];
}

// MARK: - UIWebViewDelegate

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
}

// MARK: - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    } else {
        NSArray *arr = [self.datasource valueForKey:@"items"];
        return arr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;
    if (section == 0) {
        if (row == 0) {
            ZCLogisticsExpressInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZCLogisticsExpressInfoCell" forIndexPath:indexPath];
            cell.dict = self.datasource;
            return cell;
        } else {
            ZCLogisticsSetipCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZCLogisticsSetipCell" forIndexPath:indexPath];
            NSInteger status = [[self.datasource valueForKey:@"status"] intValue];
            cell.status = status;
            return cell;
        }
    } else {
        NSArray *arr = [self.datasource valueForKey:@"items"];
        ZCLogisticsitemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZCLogisticsitemCell"
                                                                    forIndexPath:indexPath];
        cell.dict = arr[row];
        if (row == 0) {
            cell.isFirst = YES;
            cell.isLast = NO;;
        } else if (row == arr.count - 1){
            cell.isFirst = NO;
            cell.isLast = YES;;
        } else {
            cell.isFirst = NO;
            cell.isLast = NO;;
        }
        return cell;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UINib *nib = [UINib nibWithNibName:@"ZCOrderCheckSectionHeadView" bundle:nil];
        ZCOrderCheckSectionHeadView *view = [nib instantiateWithOwner:self options:nil].firstObject;
        view.title = @"物流详情";
        return view;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 10.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    if (section == 0) {
        return 95.0f;
    } else {
        return UITableViewAutomaticDimension;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 45;
    }
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

// MARK: - NET

- (void)netFetchLogistiscInfo {
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [ZCOrderViewModel fetchLogisticsInfo:self.logisticId expressId:self.expressId complete:^(ZCErrorCode status, NSString *message, id data) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        });
        if (status == kHXSNoError) {
            weakSelf.datasource = data;
            [weakSelf.tableView reloadData];
        } else if (status == kHXSInvalidTokenError) {
//            WZGLoginVC *vc = [[WZGLoginVC alloc] init];
//            [vc setGoodsVCCallBack:^(NSInteger type) {
//                [weakSelf netFetchLogistiscInfo];
//            }];
//            UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
//            nvc.modalPresentationStyle = UIModalPresentationFullScreen;
//            [weakSelf.navigationController presentViewController:nvc animated:YES completion:nil];
        } else {
            [weakSelf.view makeToast:message duration:2.0 position:CSToastPositionCenter];
        }
    }];
}


@end
